home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Belgian Amiga Club - ADF Collection
/
BS1 part 05.zip
/
BS1 part 5
/
IM_3 .adf
/
Exec
/
piarc.LZH
/
jpeg.rexx
< prev
next >
Wrap
OS/2 REXX Batch file
|
1992-06-22
|
9KB
|
379 lines
/*
* JPEG.rexx
*
* Written by: Ben Williams
* Last Update: March 4th, 1992
* For: Black Belt Systems image processing series IM, IM F/c, and IP.
* ---------------------------------------------------------------------------
* Revision: 1.07
*/
parse arg var1
/*
* open rexxsupport.library -- needed for some functions
*/
if ~show('L',"rexxsupport.library") then do
if addlib('rexxsupport.library',0,-30,0) then do
/* everything's ok */
end;
else do
say 'We Have A Library Problem, Unable To Load "rexxsupport.library"';
say 'Cannot operate JPEG.rexx without this library - sorry!';
exit 10;
end;
end;
/*
* This will automatically direct the script to the proper
* software, if it is running.
*/
prtnme = 'IP_Port'; /* assume Image Professional */
if show('P','IP_Port') = 0 then do
if show('P','IM_Port') = 0 then do
say "Can't find image processor's ARexx port!!!"; /* not running? */
say "This script requires IP, IM or IM F/c to run!";
exit(20);
end;
else do
prtnme = 'IM_Port'; /* That's the thing about assumptions... */
end; /* We make em, user's break em. */
end;
/*
* This code attempts to read a file called "picmdpath" from REXX:
* If it can't find it, the script will assume that the commands
* associated with this PI Module are in "c:". If the file exists,
* the script will look in the path that is specified in the file.
* If you create this file, you MUST put a complete, correct path
* in it; if the commands are in a sub-directory, you have to put
* the trailing slash on the path (like, device:dir/).
*
*/
cmdpath = 'c:';
if open(fhandle,'rexx:picmdpath','read') then /* open the file */
do
cmdpath = readln(fhandle);
call close(fhandle); /* close the file */
end
/*
* Prompt user - load, or save?
*/
if var1 = 'load' then do
pick = 1
end
else if var1 = 'save' then do
pick = 2
end
else do
address(prtnme);
'tofront';
options results;
'gadgets "Load","JFIF/JPEG","Save","JFIF/JPEG"';
pick = result;
options;
address;
end;
if pick=0 then do
exit 0;
end;
/*
* PI driver for compressor
*/
if pick=2 then do /* compression */
bufname = 'image';
strn = ' -q ';
address(prtnme);
options results;
'askprop "Compression/Damage Factor","0","0","100"';
qfact = result;
'askyn "Good compression","Best Compression"';
compr = result;
options;
if compr = 1 then do
strn = ' -E'||strn;
end;
prevpath = 'ram:'; /* put user in ram to start with... */
if show('C',jpegpath) = 1 then do
prevpath = getclip(jpegpath);
end;
options results;
'current';
bufdata = result; /* get name of buffer, if there is one */
parse var bufdata bname ',' bnum ',' bx ',' by ',' btot ',' bmem ',' bparname ',' bparnum;
if bname ~= '<none>' then do
bufname = bname;
end;
if (length(bufname) > 4) then do
epos = pos('.jpg',bufname,length(bufname)-4);
if epos ~= 0 then do
bufname = left(bufname,epos-1)
end
end;
'filerequest "'||prevpath||'","'||bufname||'",".jpg","Save JPEG"';
jpegfile = result;
options;
if jpegfile = 'FR_CANCELLED' then do
address(prtnme);
'imtofront';
exit 0;
end;
jpegfile = expandfilename(jpegfile); /* make filename complete path */
thispath = gimmepath(jpegfile);
call setclip(jpegpath,thispath);
address(prtnme);
options results;
'jackin';
jackadr = result;
options;
'wbtofront';
address command cmdpath||'wrjpg >"'||jpegfile||'" -j'||jackadr||strn||qfact;
address(prtnme);
'imtofront';
address;
exit 0;
end;
else do /* decompression */
/*
* Setup default path
*/
prevpath = 'ram:'; /* put user in ram to start with... */
/*
* Now, get old path if it exists
*/
if show('C',jpegpath) = 1 then do
prevpath = getclip(jpegpath);
end;
address(prtnme);
options results;
'current';
bufdata = result; /* get name of buffer, if there is one */
parse var bufdata bname ',' bnum ',' bx ',' by ',' btot ',' bmem ',' bparname ',' bparnum;
if bname ~= '<none>' then do
bufname = bname;
end;
if (length(bufname) > 4) then do
epos = pos('.jpg',bufname,length(bufname)-4);
if epos ~= 0 then do
bufname = left(bufname,epos-1)
end
end;
'filerequest "'||prevpath||'","'||bufname||'",".jpg","Load JPEG"';
jpegfile = result;
options;
if jpegfile = 'FR_CANCELLED' then do
address(prtnme);
'imtofront';
exit 0;
end;
jpegfile = expandfilename(jpegfile);
thispath = gimmepath(jpegfile);
call setclip(jpegpath,thispath);
fileinfo = statef(jpegfile);
parse var fileinfo fitype fibytes fiblocks fiflags fidays fimins fiticks ficomment;
if fitype = '' then do
'message Cannot locate "'jpegfile'" for processing';
exit 10;
end;
if fibytes < 170 then do
"message This file is too small to be a JFIF formatted JPEG file";
exit 10;
end;
if fitype = 'DIR' then do
'message Must specify a file, not a directory';
exit 10;
end;
/*
* at this point, we have at least some assurance that we
* have a real JPEG file to work with. Now, we need to look into
* the file and see how big the image is, so we can open a new
* buffer of the appropriate size.
*/
call open(fhandle,jpegfile,'read'); /* open the file */
if rvalue() ~= 65496 then do /* FFD8 */
'message Not a JFIF file! - Initial read fails';
call close(fhandle);
exit(0);
end;
work=1;
do while work = 1
thisid = rvalue();
thisln = rvalue();
thisda = readch(fhandle,thisln-2);
select
when thisid = 65472 | thisid < 65488 then do /* FFC0-FFCF (SOF0-15) */
height = c2d(substr(thisda,2,2));
width = c2d(substr(thisda,4,2));
work=0;
end;
when thisid = 65504 then do /* FFE0 */
fid = left(thisda,4);
if fid ~= 'JFIF' then do
'message Not a JFIF file!';
call close(fhandle);
exit 0;
end;
end;
otherwise do
nop;
end;
end;
end;
call close(fhandle); /* close the file */
if height < 0 then do
"message Bad height: "||height;
exit 0;
end;
if height > 32767 then do
"message Bad height: "||height;
exit 0;
end;
if width < 0 then do
"message Bad width: "||width;
exit 0;
end;
if width > 32767 then do
"message Bad width: "||width;
exit 0;
end;
address(prtnme);
'imtofront'; /* show user the IM screen */
/* is there already a primary buffer??? */
options results;
'current';
bufdata = result;
options;
parse var bufdata bname ',' bnum ',' bx ',' by ',' btot ',' bmem ',' bparname ',' bparnum
if bname ~= '<none>' then do
address(prtnme);
options results;
'askyn '||'"Replace Primary ['||bname||']" "New As Primary"'
prefs = result;
options;
address;
if prefs = 0 then do
address(prtnme);
'killbuff '||bnum; /* this kills the Primary Buffer */
address;
end;
end;
/* New buffer is created at current resolution */
address(prtnme);
'autoredraw 0';
options results;
'newbuf '||width||' '||height;
if rc ~= 0 then do
"message Can't allocate buffer!";
'autoredraw 1';
exit 0;
end
bnum = result;
'rename '||bnum||' 'gxname;
'newcurrent '||bnum;
address;
address(prtnme);
options results;
'jackin';
jackadr = result;
options;
'wbtofront';
'lockimage '||bnum;
address command cmdpath||'rdjpg -j'||jackadr||' "'||jpegfile||'"';
'unlockimage '||bnum;
address(prtnme);
'imtofront';
'redraw';
address;
exit 0;
end;
/*
* gimmepath
*
* This takes the provided argument and sucks the path out of it, then
* returns that path to the caller, sans file name.
*/
gimmepath:
arg fullnamegx;
tempgx = reverse(fullnamegx);
lengx = length(fullnamegx); /* get length of string */
slashdex = index(tempgx,'/'); /* first occurance of '/' from right */
colondex = index(tempgx,':'); /* first occurance of ':' from right */
seploc = 0; /* assumes current dir, no path supplied */
if slashdex ~= 0 then do /* we assume we are in a DIR */
seploc = (lengx - slashdex)+1;
end;
else do
if colondex ~= 0 then do /* we assume we are on a device */
seploc = (lengx - colondex)+1;
end;
end;
gxname = substr(fullnamegx,seploc+1); /* if you ever need it */
gxpath = left(fullnamegx,seploc);
return(gxpath);
/*
* Since this script can't be expected to know where the CD of the user
* is when this cmd is invoked, we have to check the path the user
* provides - if it's not specified right from a root, then we have
* to make it a complete specification from the root.
*/
expandfilename:
parse arg jfile;
if index(jfile,':') = 0 then do
curdir = pragma(D);
if right(curdir,1) ~= ':' then do
if right(curdir,1) ~= '/' then do
if curdir ~= '' then do
curdir = curdir || '/';
end;
end;
end;
jfile = curdir||jfile;
end;
return(jfile);
rvalue:
wordnum = c2d(readch(fhandle,1)) * 256;
wordnum = wordnum + c2d(readch(fhandle,1));
return wordnum;